package com.coder520.service.impl;

import com.coder520.common.ServerResponse;
import com.coder520.dao.PermissionMapper;
import com.coder520.dao.RoleMapper;
import com.coder520.pojo.Permission;
import com.coder520.pojo.Role;
import com.coder520.pojo.User;
import com.coder520.service.IPermissionService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ASUS on 2017/7/14.
 */
@Service("iPermissionService")
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;


    public List<Permission> findPerAll(){
        List<Permission> permissionList =  permissionMapper.selectAll();
        if (CollectionUtils.isNotEmpty(permissionList)){
            return permissionList;
        }
        return null;
    }



    public ServerResponse findRoleAll(){
        List<Role> roleList = roleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(roleList)){
            return ServerResponse.createBySuccess(roleList);
        }
        return ServerResponse.createBySuccess();
    }

    @Transactional
    public void updateRole(Long roleid,String[] permissionid,String dsec){
        if(roleid!=null){
            if(permissionid!=null ){
                List<Long> per_ids = Lists.newArrayList();
                for (String s : permissionid){
                    Long per_id = Long.parseLong(s);
                    per_ids.add(per_id);
                }
                permissionMapper.deleteRole_PermissionByRoleId(roleid);
                permissionMapper.insertRole_Permission(roleid,per_ids);
                Role role = new Role();
                role.setRoleid(roleid);
                role.setDescription(dsec);
                roleMapper.updateByPrimaryKeySelective(role);
                return;
            }
        }
        throw new RuntimeException("修改权限失败");
    }

    public void addRole(String[] permissionid,String desc){
        if(permissionid!=null ){
            List<Long> per_ids = Lists.newArrayList();
            for (String s : permissionid){
                Long per_id = Long.parseLong(s);
                per_ids.add(per_id);
            }
            Role role = new Role();
            role.setDescription(desc);
            roleMapper.insert(role);
            permissionMapper.insertRole_Permission(role.getRoleid(),per_ids);
            return;
        }

    }


    public List<Role> findAll(){
        List<Role> roleList = roleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(roleList)){
             roleList = assmbleRole(roleList);
             return roleList;
        }
        return null;
    }

    @Transactional
    public void addPer(String description,String permission){
        permission = permission.trim();
        description = description.trim();
        Permission per = new Permission();
        per.setPermission(permission);
        per.setDescription(description);
        int row = permissionMapper.insert(per);
        if (row==0){
            throw new RuntimeException("新增权限失败");
        }
    }

    public ServerResponse findPermissionByRoleId(Long roleid){
        if(roleid!=null){
            List<Permission> permissionList = permissionMapper.selectByRoleId(roleid);
            if(CollectionUtils.isNotEmpty(permissionList)){
                return ServerResponse.createBySuccess(permissionList);
            }
        }
        return ServerResponse.createBySuccess(null);
    }



    @Transactional
    public void update(Long per_id,String description,String permission){
        if(per_id!=null){
            permission = permission.trim();
            description = description.trim();
            Permission per = new Permission();
            per.setPermissionid(per_id);
            per.setPermission(permission);
            per.setDescription(description);
            int row = permissionMapper.updateByPrimaryKeySelective(per);
            if(row==0){
                throw new RuntimeException("修改失败");
            }
        }
    }

    @Transactional
    public void del(Long id){
        if(id!=null){
            permissionMapper.deleteByPrimaryKey(id);
        }
    }


    private List<Role> assmbleRole(List<Role> roleList){
        for(Role role : roleList){
            List<Permission> permissionList = permissionMapper.selectByRoleId(role.getRoleid());
            if(CollectionUtils.isNotEmpty(permissionList)){
                role.setPermissions(permissionList);
            }else {
                role.setPermissions(null);
            }
        }
        return roleList;

    }
}
